
set rmsg on
set more off

************************************************************************
*
*	NOTE:		This do-file uses as input the estimates from the model
*				and the Compustat sample. 
*				It then merges the estimates, which are at the group-level,
*				with the Compustat sample, which is at the gvkey-fyear-level.
*
*************************************************************************

capture program drop _all
clear
clear matrix


* Define paths

	global 	data	"..."
		
	
	
**********************************************************
**** Beginn: Merge of parameters and Compustat sample ****
**********************************************************	
	
	**** These estimates come from the estimation
	use "$data/GMMSV_estimates.dta",clear
	
	winsor2 sigma_P, replace cuts(1 90)
	winsor2 sigma_A, replace cuts(1 90)
	winsor2 rho, replace cuts(1 99)
	winsor2 mu, replace cuts(10 90)
	
	tabstat rho sigma_P sigma_A mu, stats(n mean sd min p5 p25 p50 p75 p95 max) c(s)	
	
	sort sic3_by_g_cf5_lagat
	save "$data/GMMSV_estimates_winsorized.dta", replace
	
**** 1. Load and merge the data using the gvkey-to-group mapping
	use "$data/GMMSV_sample.dta", clear
	
	sort gvkey 
	merge m:1 gvkey using "$data/GMMVS_gvkeytogroup.dta"
	keep if _merge == 3
	drop _merge

	sort sic3_by_g_cf5_lagat	
	merge m:1 sic3_by_g_cf5_lagat using "$data/GMMSV_estimates_winsorized.dta" 
	keep if _merge == 3
	drop _merge
		

**** 2. Orthogonalize variables 
	reg mbl1 rho sigma_P sigma_A
	predict mbresid5, resid

	save "$data/GMMSV_reg_sample.dta", replace


**********************************************************
**** End: Merge of parameters and Compustat sample ****
**********************************************************		
	
	
****************************************************************
**** Beginn: Descriptive statistics of firm-level variables ****
****************************************************************	
	
	use "$data/GMMSV_reg_sample.dta", clear
	

	tabstat  cf5_at0 cash_savings_at ch_cash_at equity_issue_at gross_eq_issue_dummy net_equity_issue2_at eq_issue_dummy /*
	*/		 net_ltdebt_issue_at ltdebt_issue_dummy mb l_a cf5_lagat sd_indcf5_lagat,     stats(n mean sd p5 p25 p50 p75 p95) c(s)	

	
****************************************************************
**** End: Descriptive statistics of firm-level variables ****
****************************************************************
	
	
	
	